/*
 * Copyright (c) 2023-2299 深圳市阿毛科技有限公司 All rights reserved.
 *
 * https://www.lamyz.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.lam.framework.bean.service;

import java.io.IOException;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lam.common.components.file.FileConstant;
import com.lam.common.components.file.FileDownloadUtil;
import com.lam.common.components.file.model.FileBusiTypeEnum;
import com.lam.common.components.file.service.AttachFileService;
import com.lam.common.context.TokenUtils;
import com.lam.common.exception.BusinessException;
import com.lam.common.utils.DateUtil;
import com.lam.common.utils.StringUtils;
import com.lam.common.utils.file.FileUploadUtil;
import com.lam.common.utils.uuid.IdUtils;
import com.lam.framework.bean.entity.SysFile;
import com.lam.framework.bean.mapper.FileMapper;

import cn.hutool.core.io.FileUtil;

/**
 */
@Service
public class AttachFileServiceImpl implements AttachFileService {

	private final Logger logger = LoggerFactory.getLogger(getClass());
	 
	@Autowired
    private FileMapper fileMapper;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String uploadFile(MultipartFile file, String busiModule) {
		
		try {
			String extName = FileUtil.extName(file.getOriginalFilename());
			String fileName = String.format("%s/%s_%s.%s", DateUtil.formatDate(new Date(), "yyyyMM"), DateUtil.formatDate(new Date(), DateUtil.PATTERN_YYYYMMDD),
					IdUtils.simpleUUID(), extName);
			if(StringUtils.isNotBlank(busiModule)) {
				fileName = busiModule + "/" + fileName;
			}
			SysFile sysFile = new SysFile();
			sysFile.setFilePath(fileName);
			sysFile.setFileSize(file.getBytes().length);
			sysFile.setBusiType(FileBusiTypeEnum.UPLOAD.getCode());
			sysFile.setLocation(FileUploadUtil.getUploadType());
			sysFile.setStatus(FileConstant.STATUS_NORMAL);
			sysFile.setUseFlag(FileConstant.USE_FLAG_NO);
			sysFile.setFileType(extName);
			sysFile.setSign(FileDownloadUtil.sign(fileName));
			sysFile.setKeyMd5(FileDownloadUtil.getKeyMd5());
			sysFile.setCreateTime(new Date());
			sysFile.setCreateBy(TokenUtils.getLoginUserid());
			FileUploadUtil.uploadFile(file, fileName);
			fileMapper.insert(sysFile);
			return fileName;
		} catch (IOException e) {
			logger.error("保存上传的文件失败", e);
			throw new BusinessException("上传失败");
		}
	}

	@Override
	@Transactional
	public void deleteFile(String fileName){
		
		FileUploadUtil.deleteLocal(fileName);
		
		QueryWrapper<SysFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("file_path", fileName);
		queryWrapper.eq("location", FileUploadUtil.getUploadType());
		SysFile sysFile = new SysFile();
		sysFile.setStatus(FileConstant.STATUS_DELETED);//已删除
		fileMapper.update(sysFile, queryWrapper);
	}
}
